package com.github.codemperor.core.aws;

import com.github.codemperor.core.aws.s3.S3Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.regions.Region;

@Configuration
public class AwsConfig {
    private Logger log = LoggerFactory.getLogger(this.getClass());

    private String accessKeyId;
    private String secretKey;
    private String region;

    private AwsBasicCredentials createAWSCredentials() {
        if (StringUtils.isEmpty(accessKeyId) || StringUtils.isEmpty(secretKey)) {
            return null;
        }
        return AwsBasicCredentials.create(accessKeyId, secretKey);
    }

    @Bean
    public S3Utils s3Utils() {
        log.info("[createAWSCredentials], accessKeyId: {}, secretKey: {}, region: {}", accessKeyId, secretKey, region);
        Region reg = Region.of("us-east-2");
        if (null != region && !region.equals("")) {
            reg = Region.of(region);
        }
        return new S3Utils(createAWSCredentials(), reg);
    }

    public String getAccessKeyId() {
        return accessKeyId;
    }

    public void setAccessKeyId(String accessKeyId) {
        this.accessKeyId = accessKeyId;
    }

    public String getSecretKey() {
        return secretKey;
    }

    public void setSecretKey(String secretKey) {
        this.secretKey = secretKey;
    }

    public String getRegion() {
        return region;
    }

    public void setRegion(String region) {
        this.region = region;
    }
}
